package com.ln.lnzw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ln.lnzw.R;
import com.ln.lnzw.adapter.AnnouncementAdapter;
import com.ln.lnzw.adapter.ApprovalPublicityAdapter;
import com.ln.lnzw.base.BaseFragment;
import com.ln.lnzw.bean.Bean;
import com.ln.lnzw.bean.PublicityBean;
import com.ln.lnzw.net.HttpMethods;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/4/24.
 */

public class ApprovalPublicityFragment extends BaseFragment {

    @BindView(R.id.rv_recyclerview)
    RecyclerView rvRecyclerview;
    Unbinder unbinder;
    private ApprovalPublicityAdapter approvalPublicityAdapter;
    private List<PublicityBean.ResultBean.ListBean> mList = new ArrayList<>();
    private String name;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.approval_publicity_fragment_activity, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        name=getArguments().getString("name");
        Log.i("123", "onViewCreated: "+name);
        initView();
        initData();

    }

    private void initData() {
        HttpMethods.getInstanceCenter().info.getpublicity(name,"itemList","1","15")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PublicityBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(PublicityBean bean) {
                        if (mList!= null || !mList.isEmpty()){mList.clear();}
                        mList.addAll(bean.getResult().getList());
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                        approvalPublicityAdapter.notifyDataSetChanged();
                    }
                });


    }

    private void initView() {
        rvRecyclerview.setLayoutManager(new LinearLayoutManager(activity));
        approvalPublicityAdapter = new ApprovalPublicityAdapter(R.layout.item_approval_publicity, mList);
        rvRecyclerview.setAdapter(approvalPublicityAdapter);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
